// Theme.kt
package com.example.nutritionmanager.ui.theme

import android.content.res.Configuration
import android.os.Build
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.structuralEqualityPolicy
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext

// 主题模式枚举
enum class ThemeMode {
    LIGHT, DARK, SYSTEM
}

// 全局主题状态管理
@Stable
object AppTheme {
    private val _themeMode = mutableStateOf(ThemeMode.SYSTEM, structuralEqualityPolicy())

    var themeMode: ThemeMode
        get() = _themeMode.value
        set(value) {
            _themeMode.value = value
        }
}

// 颜色方案定义
private val GreenPrimary = Color(0xFF006E06)
private val GreenSecondary = Color(0xFF389316)
private val GreenTertiary = Color(0xFF4CAF50)

private val BluePrimary = Color(0xFF2196F3)
private val BlueSecondary = Color(0xFF64B5F6)
private val BlueTertiary = Color(0xFF90CAF9)

private val DarkColorScheme = darkColorScheme(
    primary = BluePrimary,
    secondary = BlueSecondary,
    tertiary = BlueTertiary,
//    background = Color(0xFF121212),
    background = Color.Black,
    surface = Color(0xFF252626),
    onPrimary = Color.White,
    onSecondary = Color.White,
    onTertiary = Color.White,
    onBackground = Color.White,
    onSurface = Color.White,
    surfaceVariant = Color(0xFF464646)
)

private val LightColorScheme = lightColorScheme(
    primary = GreenPrimary,
    secondary = GreenSecondary,
    tertiary = GreenTertiary,
    background = Color(0xFFf1f1f1),
    surface = Color.White,
    onPrimary = Color.White,
    onSecondary = Color.Black,
    onTertiary = Color.Black,
    onBackground = Color.Black,
    onSurface = Color.Black,
    surfaceVariant = Color(0xFFEEEEEE)
)

@Composable
fun NutritionManagerTheme(
    content: @Composable () -> Unit
) {
    val context = LocalContext.current
    val configuration = context.resources.configuration

    // 在Composable上下文中判断主题
    val useDarkTheme = when (AppTheme.themeMode) {
        ThemeMode.LIGHT -> false
        ThemeMode.DARK -> true
        ThemeMode.SYSTEM -> (configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES
    }

    val dynamicColor = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S

    val colorScheme = when {
        dynamicColor && useDarkTheme -> dynamicDarkColorScheme(context).copy(
            primary = BluePrimary,
            secondary = BlueSecondary,
            tertiary = BlueTertiary,
            background = DarkColorScheme.background, // 添加背景色覆盖
            surface = DarkColorScheme.surface,
            onPrimary = DarkColorScheme.onPrimary,
            onSecondary = DarkColorScheme.onSecondary,
            onTertiary = DarkColorScheme.onTertiary,
            onBackground = DarkColorScheme.onBackground,
            onSurface = DarkColorScheme.onSurface,
            surfaceVariant = DarkColorScheme.surfaceVariant
        )
        dynamicColor && !useDarkTheme -> dynamicLightColorScheme(context).copy(
            primary = GreenPrimary,
            secondary = GreenSecondary,
            tertiary = GreenTertiary,
            background = LightColorScheme.background, // 添加背景色覆盖
            surface = LightColorScheme.surface,
            onPrimary = LightColorScheme.onPrimary,
            onSecondary = LightColorScheme.onSecondary,
            onTertiary = LightColorScheme.onTertiary,
            onBackground = LightColorScheme.onBackground,
            onSurface = LightColorScheme.onSurface,
            surfaceVariant = LightColorScheme.surfaceVariant
        )
        useDarkTheme -> DarkColorScheme
        else -> LightColorScheme
    }

    MaterialTheme(
        colorScheme = colorScheme,
        typography = Typography,
        content = content
    )
}
